package com.duguyuan.client;

import com.duguyuan.utils.MessageProtocol;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class LoginFrame extends JFrame {

    private static final int LOGIN_WIDTH = 600;
    private static final int LOGIN_HEIGHT = 400;
    private static final Font LABEL_FONT = new Font("黑体", Font.BOLD, 20);
    private static final Font TEXTFIELD_FONT = new Font("黑体", Font.BOLD, 20);
    private static final Font BUTTON_FONT = new Font("黑体", Font.BOLD, 20);

    private ClientService clientService;

    private LoginView loginView;
    JTextField ipAddressTextField;
    JTextField portTextField;
    JTextField usernameTextField; // 用户名输入框
    JPasswordField passwordTextField; // 密码输入框
    JButton loginButton; // 登录按钮
    JButton resetButton; // 重置输入按钮
    JButton registerButton; // 注册按钮


    public LoginFrame() {
        clientService = new ClientService(this);
        init();
    }

    public void init() {
        this.setTitle("登录界面");
        this.setSize(LOGIN_WIDTH, LOGIN_HEIGHT);
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        loginView = new LoginView();
        this.setContentPane(loginView);
        this.addActionListener();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void loginAction() {
        try {
            clientService.initConnection();
            if ("".equals(usernameTextField.getText()) || "".equals(String.valueOf(passwordTextField.getPassword()))) {
                JOptionPane.showMessageDialog(this, "用户名或密码不能为空", "客户端消息提示", JOptionPane.INFORMATION_MESSAGE);
            }else {
                clientService.sendMessage(MessageProtocol.LOGIN_ROUND, usernameTextField.getText() + MessageProtocol.SEPARATOR + String.valueOf(passwordTextField.getPassword()));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void addActionListener() {
        /*loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginAction();
            }
        });*/
        Action userLoginAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginAction();
            }
        };
        loginButton.addActionListener(userLoginAction);
        passwordTextField.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "login");
        passwordTextField.getActionMap().put("login", userLoginAction);
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    clientService.initConnection();
                    if ("".equals(usernameTextField.getText()) || "".equals(String.valueOf(passwordTextField.getPassword()))) {
                        JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码不能为空", "登录窗口消息", JOptionPane.INFORMATION_MESSAGE);
                    }else {
                        clientService.sendMessage(MessageProtocol.REGISTER_ROUND, usernameTextField.getText() + MessageProtocol.SEPARATOR + String.valueOf(passwordTextField.getPassword()));
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                usernameTextField.setText("");
                passwordTextField.setText("");
            }
        });
    }

    private class LoginView extends JPanel {

        public LoginView() {
            init();
        }

        public void init() {
            this.setBounds(0, 0, LOGIN_WIDTH, LOGIN_HEIGHT);
            this.setLayout(null);
            this.addElement();
        }

        public void addElement() {
            // 登录界面标题标签设置
            JLabel titleLabel = new JLabel("多人聊天系统登录界面");
            titleLabel.setBounds(100, 30, 400, 50);
            titleLabel.setFont(new Font("黑体", Font.BOLD, 38));
            this.add(titleLabel);

            // 服务器IP标签
            JLabel serverLabel = new JLabel("服务器: ");
            serverLabel.setBounds(125, 130, 100, 30);
            serverLabel.setFont(LABEL_FONT);
            this.add(serverLabel);
            // IP地址输入框
            ipAddressTextField = new JTextField(clientService.serverIPAddress);
            ipAddressTextField.setBounds(225, 130, 160, 30);
            ipAddressTextField.setFont(TEXTFIELD_FONT);
            this.add(ipAddressTextField);
            // IP地址与端口号分隔符“ : ”
            JLabel separatorLabel = new JLabel(":");
            separatorLabel.setBounds(385, 130, 20, 30);
            separatorLabel.setFont(LABEL_FONT);
            this.add(separatorLabel);
            // 端口号输入框
            portTextField = new JTextField(clientService.serverPort + "");
            portTextField.setBounds(395, 130, 80, 30);
            portTextField.setFont(TEXTFIELD_FONT);
            this.add(portTextField);

            // 用户名标签
            JLabel usernameLabel = new JLabel("用户名: ");
            usernameLabel.setBounds(125, 180, 100, 30);
            usernameLabel.setFont(LABEL_FONT);
            this.add(usernameLabel);
            // 用户名输入框
            usernameTextField = new JTextField();
            usernameTextField.setBounds(225, 180, 250, 30);
            usernameTextField.setFont(TEXTFIELD_FONT);
            this.add(usernameTextField);

            // 密码标签
            JLabel passwordLabel = new JLabel("密  码: ");
            passwordLabel.setBounds(125, 230, 100, 30);
            passwordLabel.setFont(LABEL_FONT);
            this.add(passwordLabel);
            // 密码输入框
            passwordTextField = new JPasswordField();
            passwordTextField.setBounds(225, 230, 250, 30);
            this.add(passwordTextField);

            // 登录按钮
            loginButton = new JButton("登录");
            loginButton.setBounds(150, 300, 80, 30);
            loginButton.setFont(BUTTON_FONT);
            this.add(loginButton);
            // 注册按钮
            registerButton = new JButton("注册");
            registerButton.setBounds(260, 300, 80, 30);
            registerButton.setFont(BUTTON_FONT);
            this.add(registerButton);
            // 重置按钮
            resetButton = new JButton("重置");
            resetButton.setBounds(370, 300, 80, 30);
            resetButton.setFont(BUTTON_FONT);
            this.add(resetButton);
        }
    }
}